package com.bigluo.views;

import com.bigluo.dao.domain.User;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * Created by luojun on 2017/9/7.
 */
public class UserExcelView extends AbstractExcelView {
    public void buildExcelDocument(Map<String, Object> map, HSSFWorkbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //attachment
        //HSSFWorkbook book = new HSSFWorkbook(new FileInputStream("")); 读取上传文件
        HSSFCellStyle headerStyle = workbook.createCellStyle();  //标题样式
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont headerFont = workbook.createFont();
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerFont.setFontHeightInPoints((short)11);
        headerStyle.setFont(headerFont);
        String fileName = new String("userListExcel".getBytes(),"ISO8859-1");
        httpServletResponse.setHeader("Content-Disposition","inline;filename="+fileName+".xls");
        List<User> userList = (List<User>)map.get("userList");
        HSSFSheet sheet = workbook.createSheet("用户列表");
        sheet.autoSizeColumn(3,true);
        sheet.autoSizeColumn(4,true);
        HSSFRow headRow = sheet.createRow(0);
        HSSFCell cell0 = headRow.createCell(0);
        cell0.setCellStyle(headerStyle);
        cell0.setCellValue("编号");

        HSSFCell cell1 = headRow.createCell(1);
        cell1.setCellStyle(headerStyle);
        cell1.setCellValue("姓名");

        HSSFCell cell2 = headRow.createCell(2);
        cell2.setCellStyle(headerStyle);
        cell2.setCellValue("年龄");

        HSSFCell cell3 = headRow.createCell(3);
        cell3.setCellStyle(headerStyle);
        cell3.setCellValue("地址");

        HSSFCell cell4 = headRow.createCell(4);
        cell4.setCellStyle(headerStyle);
        cell4.setCellValue("密码");
        int i=1;
        for(User user : userList) {
            HSSFRow row = sheet.createRow(i++);
            row.createCell(0).setCellValue(user.getId());
            row.createCell(1).setCellValue(user.getName());
            row.createCell(2).setCellValue(user.getAge());
            row.createCell(3).setCellValue(user.getAddr());
            row.createCell(4).setCellValue(user.getPwd());
        }
        //httpServletResponse.setContentType("application/vnd.ms-excel");
        httpServletResponse.addHeader("Content-Type","application/vnd.ms-excel;utf-8");
    }


    /**
     * 设置文件下载的文件名
     * @param filename
     * @param request
     * @return
     */
    public  String encodeFilename(String filename, HttpServletRequest request) {
            /**
              * 获取客户端浏览器和操作系统信息
              * 在IE浏览器中得到的是：User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
              * 在Firefox中得到的是：User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
              */
            String agent = request.getHeader("USER-AGENT");
            try {
               if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
                    String newFileName = URLEncoder.encode(filename, "UTF-8");
                    newFileName = StringUtils.replace(newFileName, "+", "%20");
                    if (newFileName.length() > 150) {
                          newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
                          newFileName = StringUtils.replace(newFileName, " ", "%20");
                        }
           return newFileName;
                   }
               if ((agent != null) && (-1 != agent.indexOf("Mozilla")))
                     return MimeUtility.encodeText(filename, "UTF-8", "B");

               return filename;
             } catch (Exception ex) {
               return filename;
                  }
          }





}
